package http
{
	import inc.Config;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	
	public class HttpHelper
	{
		private var url:String = null;
		[Bindable]
		public var dataArray:ArrayCollection = new ArrayCollection();
		public static var basePath:String = Config.getBasePath();
		
		public function HttpHelper()
		{
		}
		
		function loginedDefault():void{
			Alert.show("已登录");
		}
		
		function notLoginDefault():void{
			Alert.show("请先登录!");
		}
		
		//检查登录
		public static function checkLogin(logined:Function, notLogin:Function):void{
			var https:HTTPService = new HTTPService();
			https.addEventListener(ResultEvent.RESULT, checkLoginResult);
			https.showBusyCursor = true;
			https.url = basePath+"member-isLogin";
			https.method = "POST";
			https.send();
			//Alert.show(https.url);
			
			function checkLoginResult(e:ResultEvent):void{
				//Alert.show("return");
				//Alert.show(e.result.resp.success);
				if(e.result.resp.success > 0){
					logined(e);
				}else{
					notLogin();
				}
			}
		}
		
		
		
		public function deleteData(url:String):void{
			this.url = url;
			if(this.url==null){
				Alert.show("请求URL不能为空！");
				return;
			}
			var https:HTTPService = new HTTPService();
			https.showBusyCursor = true;
			https.url = this.url;
			https.method = "POST";
			https.send();
		}
		
		public function load(url:String, arr:ArrayCollection=null):void{
			this.url = url;
			if(this.url==null){
				Alert.show("请求URL不能为空！");
				return;
			}
			var https:HTTPService = new HTTPService();
			https.addEventListener(ResultEvent.RESULT, loadResult);
			https.showBusyCursor = true;
			https.url = this.url;
			https.method = "POST";
			https.send();
			
			function loadResult(e:ResultEvent):void{
				if(arr != null){
					arr.removeAll();
					if(e.result.resp.size == 1){
						arr.addItem(e.result.resp.list.obj);
					}else if(e.result.resp.size > 1){
						arr = e.result.resp.list.obj;
					}
				}
				if(dataArray != null){
					dataArray.removeAll();
				}
				if(e.result.resp.size == 1){
					dataArray.addItem(e.result.resp.list.obj);
				}else if(e.result.resp.size > 1){
					dataArray = e.result.resp.list.obj;
				}
			}
		}

	}
}